﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO.Custom;
using Business;
using Entity;
using FreshCms.BackEnd.ControlsCommon;
using Utilities;

namespace FreshCms.BackEnd.Controls.Customer
{
    public partial class CustomerManager : System.Web.UI.UserControl
    {
        #region check login and permission
        InPms chkPms = new InPms();
        public Int32 panelId = 0;
        private void checkLogin()
        {
            // Check and load UC
            if (Request.QueryString["ui"] != null)
                Int32.TryParse(Request.QueryString["ui"], out panelId);
            // check Pms
            if (panelId > 0)
                chkPms = InPmsBusiness.CheckSessionPms(panelId);
            if (chkPms == null)
                Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
            // show Pms in Top Bar
            TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
            ucTopBar.BindData(chkPms);
        }
        #endregion


        public Int32 page = 1;
        public Int32 size = 10;
        public string user = "";
        public string email = "";
        public string fullname = "";
        public string phone = "";
        public string idcard = "";
        public Int32 stt = -3;
        


        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!Page.IsPostBack)
            {
                if (chkPms.PmsView == 1)
                {
                    BindStatus();
                    BindData();
                }
            }
        }

        private void BindStatus()
        {
            ddlStatus.DataSource = StatusBusiness.GetShortListStatusForCustomer();
            ddlStatus.DataTextField = "Value";
            ddlStatus.DataValueField = "Id";
            ddlStatus.DataBind();
            ddlStatus.Items[0].Attributes.Add("style", Constant.StyleDropAll);
            ddlStatus.Items[1].Attributes.Add("style", Constant.StyleDropDisable);
            ddlStatus.Items[2].Attributes.Add("style", Constant.StyleDropEnable);
        }

        private void BindData()
        {
            page = Utils.GetQuery("page", 1);
            user = Utils.GetQuery("user", "");
            email = Utils.GetQuery("email", "");
            fullname = Utils.GetQuery("fullname", "");
            phone = Utils.GetQuery("phone", "");
            idcard = Utils.GetQuery("idcard", "");
            stt = Utils.GetQuery("stt", -3);

            Int32 total = 0;
            var lst = OutCustomerBusiness.GetOutCustomerListSearch(user, email, phone, fullname, idcard, stt, page, size, ref total);
            if (lst.Count > 0)
            {
                rpt.DataSource = lst;
                rpt.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
                rpt.DataBind();
            }
            if (total > size)
            {
                Pager.Index = page;
                Pager.Cate = Regex.Replace(Utils.GetCurrentCmsPage(), "&page=(.*)", "");
            }
            else
                Pager.Visible = false;

            // Bind value to form 
            ddlStatus.SelectedValue = stt.ToString();

        }

        void rpt_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                try
                {
                    Label lblStatus = (Label)e.Item.FindControl("lblStatus");
                    PlaceHolder phVip = (PlaceHolder)e.Item.FindControl("phVip");
                    PlaceHolder phNormal = (PlaceHolder)e.Item.FindControl("phNormal");

                    OutCustomer obj = (OutCustomer)e.Item.DataItem;

                    lblStatus.Text = StatusBusiness.GetShortStatusName(obj.CusStatus);

                    if (obj.CusStatus == 1)
                        lblStatus.CssClass = "label label-success";
                    else
                        lblStatus.CssClass = "label label-danger";
                    if (obj.CusType == 0)
                        phNormal.Visible = true;
                    else
                        phVip.Visible = true;

                }
                catch (Exception ex) { }

            }
        }
    }
}